package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

public class AdminJsonServlet extends BaseServlet {
	public void upload(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out=response.getWriter();
		 
		SmartUpload smart = new SmartUpload();
		
		smart.setCharset("UTF-8");
		
		smart.initialize(getServletConfig(),request,response);
		try {
			smart.upload();
			String fileName = System.currentTimeMillis()+".jpg";
		
			smart.getFiles().getFile(0).saveAs("./images/"+fileName);
			
			out.print(Result.toClient(true, "上传成功",fileName));
		} catch (SmartUploadException e) {
			e.printStackTrace();
			out.print(Result.toClient(false, "上传失败","图片过大"));
		}
		
	}

}
